def max_sliding_window(nums, k):
    # deq keeps the idx of the num. nums of the indices in deq should be descending.
    deq, res = [], []
    for i in range(len(nums)):
        num = nums[i]
        # remove the oldest
        while deq and deq[0] < i - k + 1:
            deq.pop(0)
        while deq and nums[deq[-1]] <= num:
            deq.pop(-1)
        deq.append(i)
        if i>= k-1:
            res.append(nums[deq[0]])
    return res

print(max_sliding_window([1,3,-1,-3,5,3,6,7], 3))